﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Nizm0.QOT.Library.Options
{
    public partial class usProjectOptions : UserControl, Library.Objects.IOption
    {
        //private Properties.Settings set = Properties.Settings.Default;
        //Library.Objects.ProjectSettings _project;

        //public MainOptions()
        //{
        //    InitializeComponent();
        //    textBox_DefoultProjectLocalization.Text = Library.Settings.Instance.DefoultProjectLocation;
        //    textBox_MaxRouningProcess.Text = Library.Settings.Instance.MaxRouningProcess.ToString();// Properties.Settings.Default.MaxRouningProcess.ToString();
        //    textBox_ThisProjectLocalization.Text = null;
        //    textBox_LocalizationOfQueryFiles.Text = null;
        //    textBox_OldVersionOfProcedures.Text = null;
        //    textBox_NewVersionOfProcedures.Text = null;
        //    checkBox_WordWrap.Checked = Library.Settings.Instance.ResultWordWrap;// Properties.Settings.Default.ResultWordWrap;
        //}

        public usProjectOptions()//ref Library.Objects.ProjectSettings project)
        {
            InitializeComponent();
            Fill();
        }

        public void Fill()
        {
            //_project = project;
            groupBox1.Enabled = Settings.Instance.ProjectSettings.IsLoaded;
            if (Settings.Instance.ProjectSettings.IsLoaded)
            {
                textBox_ThisProjectLocalization.Text = Settings.Instance.ProjectSettings.ProjectLocation;
                textBox_LocalizationOfQueryFiles.Text = Settings.Instance.ProjectSettings.ProjectResultsLocation;
                listBox_Folders.DataSource = Settings.Instance.ProjectSettings.ObservedDirectories;
            }
        }

        private void bt_QueryFilesLocalization_Click(object sender, EventArgs e)
        {
            folderBrowserDialog_Localization.SelectedPath = textBox_LocalizationOfQueryFiles.Text;// Properties.Settings.Default.ProcedureLocalization;
            if (folderBrowserDialog_Localization.ShowDialog() == DialogResult.OK)
            {
                textBox_LocalizationOfQueryFiles.Text = folderBrowserDialog_Localization.SelectedPath;
                //Properties.tmpserver.Default.ProcedureLocalization = folderBrowserDialog_Localization.SelectedPath;
            }
        }

        public void SaveSettings()
        {
            Settings.Instance.ProjectSettings.ProjectLocation = textBox_LocalizationOfQueryFiles.Text.Trim();
            Settings.Instance.ProjectSettings.ProjectResultsLocation = textBox_LocalizationOfQueryFiles.Text;
            //Settings.Instance.ProjectSettings.ObservedDirectories = listBox_Folders.DataSource;

            Settings.Instance.ProjectSettings.Save();
        }

        private void button_FolderAdd_Click(object sender, EventArgs e)
        {
            using (FileFolderDialog f = new FileFolderDialog())
            {
                if (DialogResult.OK == f.ShowDialog())
                {
                    //listBox_Folders.Items.Add(folderdialog.SelectedPath);
                    Settings.Instance.ProjectSettings.AddObservedDirectories(f.SelectedPath);
                    ((CurrencyManager)listBox_Folders.BindingContext[Settings.Instance.ProjectSettings.ObservedDirectories]).Refresh();
                }
            }
            //using (FolderBrowserDialog folderdialog = new FolderBrowserDialog())
            //{

            //    folderdialog.ShowNewFolderButton = true;
            //    folderdialog.RootFolder = Environment.SpecialFolder.Desktop;
            //    if (DialogResult.OK == folderdialog.ShowDialog())
            //    {
            //        //listBox_Folders.Items.Add(folderdialog.SelectedPath);
            //        Settings.Instance.ProjectSettings.AddObservedDirectories(folderdialog.SelectedPath);
            //        ((CurrencyManager)listBox_Folders.BindingContext[Settings.Instance.ProjectSettings.ObservedDirectories]).Refresh();
            //    }
            //}
        }

        private void button_FolderRemove_Click(object sender, EventArgs e)
        {
            if (listBox_Folders.SelectedItems.Count > 0)
            {
                Settings.Instance.ProjectSettings.RemoweObservedDirectories((IEnumerable<string>)listBox_Folders.SelectedItems);
                ((CurrencyManager)listBox_Folders.BindingContext[Settings.Instance.ProjectSettings.ObservedDirectories]).Refresh();
            }
        }

        private void button_FolderEdit_Click(object sender, EventArgs e)
        {
            if (listBox_Folders.SelectedItems.Count > 0)
                using (FileFolderDialog folderdialog = new FileFolderDialog())
                {
                    //folderdialog.ShowNewFolderButton = true;
                    //folderdialog.RootFolder = Environment.SpecialFolder.Desktop;
                    folderdialog.SelectedPath = listBox_Folders.SelectedItem.ToString();
                    if (DialogResult.OK == folderdialog.ShowDialog())
                    {
                        //listBox_Folders.Items.Add(folderdialog.SelectedPath);
                        Settings.Instance.ProjectSettings.AddObservedDirectories(folderdialog.SelectedPath);
                        ((CurrencyManager)listBox_Folders.BindingContext[Settings.Instance.ProjectSettings.ObservedDirectories]).Refresh();
                    }
                }
        }

        #region IOption
        private string _optionPath = "Main Settings\\Project";
        public string OptionPath
        {
            get
            {
                return _optionPath;
            }
            set
            {
                _optionPath = value;
            }
        }

        private string _optionName = "Project";
        public string OptionName
        {
            get { return _optionName; }
        }

        private bool _optionChange = true;
        public bool OptionChanged
        {
            get { return _optionChange; }
        }

        public bool Save()
        {
            SaveSettings();
            return true;
        }

        public Library.Objects.IOption Show()
        {
            return new usProjectOptions();
        }
        #endregion
    }
}
